import Vue from "vue";
// import CryptoJS from "crypto-js";
import { JSEncrypt } from "jsencrypt";
import cookieUtils from "./cookieUtils";
const PublicKey =
  "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLBIQvCu8Fa+bqhEz+dxkwhDm707XUEZHW1qbVunbrqevTAjKwEgpSnnNyy0ha1mlq15UxSi+c/c+YBhZPnkYmgNoO1KzG6wtVAdCwpmG85S9JzzyZ8gD0o0qHSI6XRMwcYaXlV5vNZkfGos6T1OYTYO28fMEuiIl5aln/F1CEgwIDAQAB";
const PrivateKey = "";
/**
 * RSA 加密
 */
export function RSAEncrypt(string) {
  const jse = new JSEncrypt();
  jse.setPublicKey(PublicKey);
  return jse.encrypt(string);
}
/**
 * RSA 解密
 */
export function RSADecrypt(string) {
  const jse = new JSEncrypt();
  jse.setPrivateKey(PrivateKey);
  return jse.decrypt(string);
}
/**
 * @param {Function} func
 * @param {number} wait
 * @param {boolean} immediate
 * @return {*}
 */
export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result;

  const later = function () {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp;

    // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last);
    } else {
      timeout = null;
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args);
        if (!timeout) context = args = null;
      }
    }
  };

  return function (...args) {
    context = this;
    timestamp = +new Date();
    const callNow = immediate && !timeout;
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait);
    if (callNow) {
      result = func.apply(context, args);
      context = args = null;
    }

    return result;
  };
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate(data, id = "id", pid = "parentId") {
  var res = [];
  var temp = {};
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i];
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]["children"]) {
        temp[data[k][pid]]["children"] = [];
      }
      if (!temp[data[k][pid]]["_level"]) {
        temp[data[k][pid]]["_level"] = 1;
      }
      data[k]["_level"] = temp[data[k][pid]]._level + 1;
      temp[data[k][pid]]["children"].push(data[k]);
    } else {
      res.push(data[k]);
    }
  }
  return res;
}

/**
 * 清除登录信息
 */
export function clearLoginInfo() {
  cookieUtils.removeToken();
}

/**
 * URL 编码
 */
export function encodeURL(url) {
    return encodeURIComponent(url);
  }
  
  /**
 *URL 解码函数
 */
export function decodeURL(encodedUrl) {
    return decodeURIComponent(encodedUrl);
  }
  // DES加密
// export const encryptBy = (message) => {
//   var key = "12345678";
//   function encryptByDES(message, key) {
//     var keyHex = CryptoJS.enc.Utf8.parse(key);
//     var option = { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 };
//     var encrypted = CryptoJS.DES.encrypt(message, keyHex, option);
//     return encrypted.ciphertext.toString();
//   }
//   return encryptByDES(message, key);
// };
// DES解密
// export const decryptBy = (message) => {
//   var key = "12345678";
//   // DES  ECB模式解密
//   function decryptByDES(message, key) {
//     var keyHex = CryptoJS.enc.Utf8.parse(key);
//     var decrypted = CryptoJS.DES.decrypt(
//       {
//         ciphertext: CryptoJS.enc.Hex.parse(message),
//       },
//       keyHex,
//       {
//         mode: CryptoJS.mode.ECB,
//         padding: CryptoJS.pad.Pkcs7,
//       }
//     );
//     var result_value = decrypted.toString(CryptoJS.enc.Utf8);
//     return result_value;
//   }
//   return decryptByDES(message, key);
// };
